﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Services
{
	using System;
	using Falcon.Core.Collections;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;

	public class UserNoteService : IUserNoteService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UserNoteService"/> class.
		/// </summary>
		/// <param name="identityService">The identity service.</param>
		/// <param name="userNoteRepository">The user note repository.</param>
		public UserNoteService(
			IIdentityService identityService, 
			IUserNoteRepository userNoteRepository)
		{
			this.IdentityService = identityService;
			this.UserNoteRepository = userNoteRepository;
		}

		/// <summary>
		/// Gets or sets the identity service.
		/// </summary>
		/// <value>The identity service.</value>
		public IIdentityService IdentityService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the user note repository.
		/// </summary>
		/// <value>The user note repository.</value>
		public IUserNoteRepository UserNoteRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the user note by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public UserNote GetById(int id)
		{
			return this.UserNoteRepository.GetById(id);
		}

		/// <summary>
		/// Gets all user notes by specification.
		/// </summary>
		/// <param name="specification">The specification.</param>
		/// <returns></returns>
		public IPagedList<UserNote, UserNoteSpecification> GetPaged(UserNoteSpecification specification)
		{
			return this.UserNoteRepository.GetPaged(specification);
		}

		/// <summary>
		/// Creates the user note.
		/// </summary>
		/// <returns></returns>
		public UserNote Create()
		{
			return new UserNote
			{
				Created = DateTime.Now
			};
		}

		/// <summary>
		/// Inserts the specified user note.
		/// </summary>
		/// <param name="note">The note.</param>
		public void Insert(UserNote note)
		{
			note.Validate();

			note.Slug = note.Title.Slug();
			note.BodyParsed = note.Body.BBCodeToHtml(true);

			this.UserNoteRepository.Insert(note);
		}

		/// <summary>
		/// Inserts the specified user note.
		/// </summary>
		/// <param name="note">The note.</param>
		/// <param name="value">The value.</param>
		public void Insert(UserNote note, UserNoteCreate value)
		{
			value.Validate();

			var identity = this.IdentityService.Current;

			note.AuthorId = identity.Id;
			note.UserId = value.UserId;
			note.Title = value.Title;
			note.Body = value.Body;

			this.Insert(note);
		}

		/// <summary>
		/// Updates the specified user note.
		/// </summary>
		/// <param name="note">The note.</param>
		public void Update(UserNote note)
		{
			note.Validate();

			note.Slug = note.Title.Slug();
			note.BodyParsed = note.Body.BBCodeToHtml(true);

			this.UserNoteRepository.Update(note);
		}

		/// <summary>
		/// Updates the specified user note.
		/// </summary>
		/// <param name="note">The note.</param>
		/// <param name="value">The value.</param>
		public void Update(UserNote note, UserNoteUpdate value)
		{
			value.Validate();

			note.Title = value.Title;
			note.Body = value.Body;

			this.Update(note);
		}

		/// <summary>
		/// Deletes the specified user note.
		/// </summary>
		/// <param name="note">The note.</param>
		public void Delete(UserNote note)
		{
			this.UserNoteRepository.Delete(note);
		}
	}
}